import { _decorator, Button, Component, DebugView, director, error, instantiate, Label, log, Node, Prefab, warn } from 'cc';
import { ResMgr } from './ResMgr';
import { SoundMgr } from './SoundMgr';
const { ccclass, property } = _decorator;

export class Debug extends Component {
    public static Instance: Debug = null;

    private _logLabel: Label = null;
    private _windowRoot: Node = null;
    private _showAndHideBitn: Button = null;
    private _isShow: boolean = null;
    private _logLines = new Array<string>();

    protected onLoad(): void {
        if(Debug.Instance !== null){
            this.destroy();
            return;
        }

        Debug.Instance =this;
    }

    public async Init() {
        var debugPrefab: Prefab = await ResMgr.Instance.IE_GetAsset('FMDebug','DebugWindow',Prefab) as Prefab;
        var debugWindow = instantiate(debugPrefab);
        this.node.parent.addChild(debugWindow);

        if(debugWindow === null){
            return;
        }
        
        console.log('======Debug=====',debugWindow);

        this._windowRoot = debugWindow.getChildByName('WindowRoot');
        this._logLabel = this._windowRoot.getChildByName('LogContentLabel').getComponent(Label);

        this._showAndHideBitn = debugWindow.getChildByName('ShowAndHide').getComponent(Button);
        this._showAndHideBitn.node.on('click',this.OnShowAndHidDebugWindow, this);
        this._isShow = true;
        this.OnShowAndHidDebugWindow();

        director.addPersistRootNode(debugWindow);
    }

    private OnShowAndHidDebugWindow(): void{
        SoundMgr.Instance.SetSoundMute(false);
        SoundMgr.Instance.PlaySound("attack2");

        this._isShow = !this._isShow ;
        if(this._isShow){
            this._windowRoot.active = true;
        }
        else{
            this._windowRoot.active = false;
        }
    }
    
    public static Log(...opttionParams: any[]): void{
        var str = "";
        for (let i = 0; i < arguments.length;i++) {
            str += arguments[i];
        }

        log(str);
        if(Debug.Instance !== null){//debugView 已经有了
            Debug.Instance._logLines.push(str);
            Debug.Instance.UpdateContentToDebugWindow();
        }
    }

    public static Error(...opttionParams: any[]){
        var str = "";
        for (let i = 0; i < arguments.length;i++) {
            str += arguments[i];
        }

        error(str);
        if(Debug.Instance !== null){//debugView 已经有了
            Debug.Instance._logLines.push(str);
            Debug.Instance.UpdateContentToDebugWindow();
        }
    }

    public static Warning(...opttionParams: any[]): void{
        var str = "";
        for (let i = 0; i < arguments.length;i++) {
            str += arguments[i];
        }

        warn(str);
        if(Debug.Instance !== null){//debugView 已经有了
            Debug.Instance._logLines.push(str);
            Debug.Instance.UpdateContentToDebugWindow();
        }
    }

    private UpdateContentToDebugWindow(): void {
        var constentStr = "";
        var maxNum = (this._logLines.length < 20) ? this._logLines.length : 20;
        for (let i = 0; i < maxNum; i++) {
            constentStr = constentStr + this._logLines[this._logLines.length - 1- i] +"\n";
        }
        this._logLabel.string = constentStr;
    }
}


